package xin.petstore.framework.utils;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eloancn.common.constant.Constant;

/**
 * @Title: CommonUtils.java
 * @Package com.eloancn.common.utils
 * @Description: 公共工具类
 * @author zsongbo
 * @date 2016年5月19日 上午11:00:06
 * @version V1.0
 */
public class CommonUtils {
	private static Logger logger = LoggerFactory.getLogger(CommonUtils.class);
	/**
	 * @Title: JionParams
	 * @Description: 拼接url
	 * @param @param url
	 * @param @param param
	 * @param @return
	 * @return StringBuffer
	 * @throws
	 */
	public static StringBuffer JionParams(StringBuffer url, String... param) {
		if (url.indexOf("?") > -1) {
			url.append("&").append(param);
		} else {
			url.append("?").append(param);
		}
		return url;
	}

	/**
	 * @Title: isContains
	 * @Description: 判断Enumeration元素是否包含在数组内
	 * @param @param enu
	 * @param @param array
	 * @param @param startIndex
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static String getExistParam(Enumeration<String> iterator, String[] array) {
		while(iterator.hasMoreElements()) {
			String nextElement = iterator.nextElement();
			for (int i = 0; i < array.length; i++) {
				if (array[i].equals(nextElement)) {
					return array[i];
				}
			}
		}
		return null;
	}

	/**
	 * @Title: printCallStatck
	 * @Description: 堆栈信息
	 * @param @param ex
	 * @return void
	 * @throws
	 */
	public static String printCallStatck(Throwable ex) {
		StackTraceElement[] stackElements = ex.getStackTrace();
		StringBuffer sbuff = new StringBuffer();
		if (stackElements != null) {
			for (int i = 0; i < stackElements.length; i++) {
				sbuff.append(stackElements[i].getClassName() + "\t");
				sbuff.append(stackElements[i].getFileName() + "\t");
				sbuff.append(stackElements[i].getLineNumber() + "\t");
				sbuff.append(stackElements[i].getMethodName());
			}
		}

		System.out.println(sbuff.toString());
		if (sbuff.length() > 0)
			return sbuff.toString();
		else
			return "";
	}

	/**
	 * @Title: addCookie
	 * @Description: 添加cookie
	 * @param @param httpResponse
	 * @param @param key
	 * @param @param param
	 * @return void
	 * @throws
	 */
	public static void addCookie(HttpServletResponse httpResponse, String name,
			Object param) {
		if (param == null) {
			param = "";
		}
		try {
			Cookie cookie = new Cookie(name, param.toString());
			cookie.setPath(Constant.COOKIE_PATH);
			cookie.setMaxAge(Constant.COOKIE_MAX_AGE);
			cookie.setDomain(Constant.COOKIE_DOMAIN);
			httpResponse.addCookie(cookie);
		} catch (Exception e) {
			logger.error("addCookie error!");
			e.printStackTrace();
		}
	
	}

	/** 
	* @Title: getCookieByName 
	* @Description: 获取值
	* @param @param request
	* @param @param name
	* @param @return    
	* @return Cookie    
	* @throws 
	*/
	public static Cookie getCookieByName(HttpServletRequest request, String name) {
		try {
			Map<String, Cookie> cookieMap = ReadCookieMap(request);
			if (cookieMap.containsKey(name)) {
				Cookie cookie = (Cookie) cookieMap.get(name);
				return cookie;
			} 
		} catch (Exception e) {
			logger.error("getCookieByName error!");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将cookie封装到Map里面
	 * 
	 * @param request
	 * @return
	 */
	private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
		Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
		Cookie[] cookies = request.getCookies();
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				cookieMap.put(cookie.getName(), cookie);
			}
		}
		return cookieMap;
	}

	/** 
	* @Title: delCookie 
	* @Description: 删除cookie
	* @param @param request
	* @param @param response    
	* @return void    
	* @throws 
	*/
	public static void delCookie(HttpServletRequest request,
			HttpServletResponse response , String name) {
		try {
			Cookie cookie = new Cookie(name, null);
			cookie.setMaxAge(0);
			cookie.setPath(Constant.COOKIE_PATH);// 根据你创建cookie的路径进行填写
			cookie.setDomain(Constant.COOKIE_DOMAIN);
			response.addCookie(cookie);
		} catch (Exception e) {
			logger.error("delCookie error!");
			e.printStackTrace();
		}
	
	}
}
